package top.saodisheng.controller;


import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.saodisheng.pojo.Order;
import top.saodisheng.pojo.Param;
import top.saodisheng.pojo.User;
import top.saodisheng.pojo.Vehicle;
import top.saodisheng.service.*;
import top.saodisheng.util.DateUtil;
import top.saodisheng.util.Page4Navigator;
import top.saodisheng.util.Result;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author 扫地生_saodisheng
 */
@RestController
public class ForeRestController {
    @Autowired
    OrderService orderService;
    @Autowired
    PackageService packageService;
    @Autowired
    StaffService staffService;
    @Autowired
    UserService userService;
    @Autowired
    VehicleService vehicleService;


    @GetMapping("/forecounts")
    public Object getCounts() {
        Map<String, Object> map = new HashMap<>();
        map.put("oc", orderService.count());
        map.put("uc", userService.count());
        map.put("sc", staffService.count());
        map.put("vc", vehicleService.count());
        map.put("pc", packageService.count());
        return Result.success(map);
    }

    @GetMapping("/foreorders")
    public Object get() {
        Map<String, Object> map = new HashMap<>();
        map.put("users", userService.list());
        map.put("staffs", staffService.list());
        map.put("vehicles", vehicleService.list());
        map.put("packages", packageService.list());
        return Result.success(map);
    }

    // 添加订单
    @PostMapping("/foreorders")
    public Object add(@RequestBody Order bean) {
        bean.setSender(userService.get(bean.getSid()));
        bean.setConsignee(userService.get(bean.getCid()));
        bean.setVehicle(vehicleService.get(bean.getVid()));
        bean.setStaff(staffService.get(bean.getStaid()));
        bean.setPg(packageService.get(bean.getPid()));
        bean.setFreight(bean.getFreight());
        bean.setCreateDate(new Date());
        bean.setDeliveryDate(DateUtil.nextSevenDate());

        System.out.println(bean);
        orderService.add(bean);
        return bean;
    }

    // 数据统计使用，获取用于搜索的相关数据（用户和运输工具）
    @GetMapping("/forestatistics")
    public Object getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("users", userService.list());
        map.put("vehicles", vehicleService.list());
        return Result.success(map);
    }

    /**
     * 数据统计，根据传递的时间（开始、截止）和寄件人以及运输工具四个条件查询相应的订单项
     * 这里需要注意的是@RequestBody不支持getMappper方式，如果要使用Get方式的或要另行考虑
     * 同时在这里结果，扫地生并没有使用页面，原因在于JPA 中只有findAll()这个自带的函数是有返回页面的功能
     * 很明显，这里根据不同的条件查询订单项的方法是自定义声明的，扫地生并没有发现自定义JPA方法可以实现
     * 页面返回的功能（不过通过查询到的数据在进行手动页面化倒是可以实现，不过这样一来就失去的Hibernate的全自动优势了）
     * 所以在这里扫地生认为，用Mybatis或许分页就没这么麻烦了
     */
    @PostMapping("/forestatistics")
    public Object listAll(@RequestBody Param param) {
        int sid = param.getSid();
        int vid = param.getVid();
        // 处理时间
        String startTime = param.getStartTime();
        String endTime = param.getEndTime();
        startTime = startTime.length() == 0 ? "2000-01-01" : startTime;
        endTime = endTime.length() == 0 ? "2100-01-01" : endTime;

        // 获取列表
        double total = 0;       // 总价
        List<Order> beans = null;
        if (sid == -1) {
            if (vid == -1) {// 1、指更加时间查询订单
                beans = orderService.listByCreateDateBetween(DateUtil.formatDate(startTime), DateUtil.formatDate(endTime));
            } else {// 2、根据运输工具和时间查询订单
                beans = orderService.listByVehicleAndCreateDateBetween(vehicleService.get(vid),
                        DateUtil.formatDate(startTime), DateUtil.formatDate(endTime));
            }
        } else {
            if (vid == -1) {// 3、根据寄件人和时间查询订单
                beans = orderService.listBySenderAndCreateDateBetween(userService.get(sid),
                     DateUtil.formatDate(startTime), DateUtil.formatDate(endTime));
            } else {
                beans = orderService.listBySenderAndVehicleAndCreateDateBetween(userService.get(sid), vehicleService.get(vid),
                        DateUtil.formatDate(startTime), DateUtil.formatDate(endTime));
            }
        }
        for (Order o : beans) {
            total += o.getFreight();
        }

        Map<String, Object> map = new HashMap<>();

        map.put("total", total);
        map.put("beans", beans);
        return Result.success(map);
    }


    // 用户对应的订单获取，功能大致和上面一个方法一样，这是这里仅仅采用时间这个条件进行查询
    @PostMapping("/foreuserorders")
    public Object list(@RequestBody Param param) {
        int sid = param.getSid();
        // 处理时间
        String startTime = param.getStartTime();
        String endTime = param.getEndTime();
        startTime = startTime.length() == 0 ? "2000-01-01" : startTime;
        endTime = endTime.length() == 0 ? "2100-01-01" : endTime;


        // 获取列表
        double total = 0;       // 总价
        List<Order> beans = null;
        User sender = userService.get(sid);     // 寄件方
        beans = orderService.listBySenderAndCreateDateBetween(sender,
                DateUtil.formatDate(startTime), DateUtil.formatDate(endTime));
        for (Order o : beans) {
            total += o.getFreight();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sender", sender);
        map.put("total", total);
        map.put("beans", beans);
        return Result.success(map);
    }
}
